package br.com.fogas.utils.validacao;

import br.com.fogas.utils.R;
import br.com.fogas.utils.StringUtility;
import android.view.View;
import android.widget.EditText;

public class Validacao {
	
	public static boolean validateNotNull(View view, String pMessage, boolean onBlur, boolean onClickButton) {

		if (view instanceof EditText) {
			EditText edText = (EditText) view;
			
			if(onBlur) {
				//Preenchido
				boolean preenchido = StringUtility.isNotEmpty(edText.getText().toString());
				
				if (preenchido) {
					edText.setBackgroundResource(R.drawable.edit_text_bg_form);
					return true;
				} else {
					edText.setBackgroundResource(R.drawable.edit_text_error_validation);
					edText.setError(pMessage); 
					
					if(onClickButton) {
						edText.setFocusable(true);
						edText.requestFocus();
					}
					return false;
				}
			}
		}
		
		return false;
	}
	
	public static boolean validateQuantityCaracters(View view, String message, int quantity, boolean onBlur, boolean onClickButton) {

		if (view instanceof EditText) {
			EditText edText = (EditText) view;
			
			if(onBlur) {
				boolean qtdPreenchida = StringUtility.length(edText.getText().toString()) == quantity;
				
				if (qtdPreenchida) {
					edText.setBackgroundResource(R.drawable.edit_text_bg_form);
					return true;
				} else {
					edText.setBackgroundResource(R.drawable.edit_text_error_validation);
					edText.setError(message); 
					
					if(onClickButton) {
						edText.setFocusable(true);
						edText.requestFocus();
					}
					return false;
				}
			}
		}
		
		return false;
	}
	
}
